package com.app.base.app;


import com.caszx.chargingpile.R;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 
 * 所有第一次进入界面时有网络请求的父类
 * 
 */
public abstract class BaseNetViewActivity extends BaseNetActivity {


	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View view = initRootView();
		setContentView(view);
	}
	
	@Override
	protected View initLoadView() {
		View view = getLayoutInflater().inflate(R.layout.loadlayout, getContentView(),true);
		View sLoadview = view.findViewById(R.id.load_view_container);
		return sLoadview;
	}

	@Override
	protected ViewGroup initRootView() {
		rootView = new RelativeLayout(this);
		return rootView;
	}

	@Override
	protected View initErrorView() {
		View errorView = getLayoutInflater().inflate(R.layout.errorlayout, null);
		errorView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				view.setVisibility(View.GONE);
			    View loadView = getBaseLoadView();
				loadView.setVisibility(View.VISIBLE);
				onLoad();
			}
		});
		return errorView;
	}

	/**
	 * 数据请求成功后，动态加载ContentView到根布局容器
	 * <p>
	 * <b>1.先关闭LoadingView
	 * <p>
	 * <b>2.调用initOnloadedView()加载ContentView
	 * <p>
	 * <b>3.判断ContentView是否已经加载到跟布局里，false addView(onloadedView),
	 */
	protected void showContentView(boolean isRemoveLoadView) {
		if (isRemoveLoadView) {
			if (baseLoadView != null) {
				baseLoadView.setVisibility(View.GONE);
				rootView.removeView(baseLoadView);
				baseLoadView = null;
			}
		}else {
			baseLoadView.setVisibility(View.GONE);
		}

		if (baseErrorView != null) {
			baseErrorView.setVisibility(View.GONE);
			rootView.removeView(baseErrorView);
			baseErrorView = null;
		}

		// 获取数据要绑定的View
		View onloadedView = getBaseLoadedView();
		if (onloadedView.getParent() == null) {
			rootView.addView(onloadedView, new LayoutParams(
					LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT));
		}
	}
	
	protected void showContentView(){
		showContentView(true);
	}

	/**
	 * 数据加载失败后显示错误页面
	 * <p>
	 * <b>1.先关闭LoadingView
	 * <p>
	 * <b>2.动态加载错误页面布局
	 * <p>
	 * <b>3.获取跟布局加载错误页面
	 */
	@Override
	protected void showErrorView() {

		// 关闭LoadingView
		if (baseLoadView != null)
			baseLoadView.setVisibility(View.GONE);
		// 显示错误页面
		View errorView = getBaseErrorView();
		if (errorView.getParent() == null) {
			// 获取根布局
			ViewGroup contentView = getContentView();
			// 动态加载错误页面
			contentView.addView(errorView, new RelativeLayout.LayoutParams(
					LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT));
		} else {
			errorView.setVisibility(View.VISIBLE);
		}

	}

	/**
	 * 数据加载失败后显示错误页面
	 * <p>
	 * <b>1.先关闭LoadingView
	 * <p>
	 * <b>2.动态加载错误页面布局
	 * <p>
	 * <b>3.获取跟布局加载错误页面
	 */
	@Override
	protected void showErrorView(String msg) {
		this.showErrorView();
		TextView txtMsg = (TextView) baseErrorView.findViewById(R.id.txtMsg);
		txtMsg.setText(msg);
	}
	@Override
	protected void showErrorView(int msg) {
		this.showErrorView();
		TextView txtMsg = (TextView) baseErrorView.findViewById(R.id.txtMsg);
		txtMsg.setText(msg);
	}
	
	
	@Override
	protected void showMsgView( ViewGroup anchor,String msg) {
		if (baseMesgView == null) {
			View view = getLayoutInflater().inflate(
					R.layout.message_layout, anchor, true);
			baseMesgView = view.findViewById(R.id.imgMsg);
	        baseMesgView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
				}
			});
		}
		baseMesgView.setVisibility(View.VISIBLE);
		TextView txtMsg = (TextView) baseMesgView.findViewById(R.id.txtMsg);
		txtMsg.setText(msg);
		if (baseErrorView != null) {
			baseErrorView.setVisibility(View.GONE);
		}
		if(baseLoadView!=null){
			baseLoadView.setVisibility(View.GONE);
		}
	}
	

}
